package com.example;

import android.content.Context;
import android.database.DataSetObserver;
import android.net.http.AndroidHttpClient;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.*;
import net.tsz.afinal.FinalActivity;
import net.tsz.afinal.annotation.view.ViewInject;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;

import java.util.ArrayList;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: wlai
 * Date: 5/28/13
 * Time: 5:30 PM
 * To change this template use File | Settings | File Templates.
 */
public class TestActivity extends FinalActivity {

    @ViewInject(id = R.id.button, click = "btnClick")
    Button button;
    @ViewInject(id = R.id.listView)
    ListView listView;

    @ViewInject(id = R.id.editText)
    EditText editText;
    private List<String> todoList = new ArrayList<String>();
     BaseAdapter  adapter = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);    //To change body of overridden methods use File | Settings | File Templates.
        setContentView(R.layout.todolist);

//         adapter = new ArrayAdapter<String>(this, R.layout.simple_todolist_item, R.id.textView, todoList);

        adapter = new TodoListAdapter(this, todoList);
        listView.setAdapter(adapter);


    }

    public void btnClick(View v) {
        String todo = editText.getText().toString();
        if (todo != null && !todo.trim().equals("")) {
            todoList.add(todo);
            editText.setText("");
            adapter.notifyDataSetChanged();
        }
    }
}


class TodoListAdapter extends BaseAdapter {
    LayoutInflater layoutInflater;
    Context context;
    List<String> data;

    public TodoListAdapter(Context ctx, List<String> data) {
        context = ctx;
        this.data = data;
    }

    @Override
    public int getCount() {
        return data.size();
    }

    @Override
    public Object getItem(int i) {
        return data.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        view = LayoutInflater.from(context).inflate(R.layout.simple_todolist_item, null);
        TextView tv1 = (TextView) view.findViewById(R.id.textView);
        TextView btn = (TextView) view.findViewById(R.id.deleteBtn);

        tv1.setText(data.get(i));
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                data.remove(i);
                notifyDataSetChanged();
            }
        });

        return view;
    }
}